package dk.fuddi.grails

import groovy.mock.interceptor.MockFor

/**
 * @author fuddi
 */

class DatabaseConnection {
  def open() {
    println "Open real db"
  }

  def close() {
    println "Close real db"
  }
}

class MyDao {
  def getData() {
    DatabaseConnection connection = new DatabaseConnection()
    try {
      connection.open()
      println "Get data from database"
    } finally {
      connection.close()
    }
  }
}

def mockDB = new MockFor(DatabaseConnection.class)
mockDB.demand.open{ println "Open mock db"}
mockDB.demand.close{ println "Close mock db"}


MyDao dao = new MyDao()

mockDB.use {
  dao.data
}